﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;
using MyCommerce.Presentation.Stores.IViews;
using MyCommerce.Business.Stores;
using MyCommerce.Business.GeneralParameters;
using Innova.Microfinanzas.BusinessEntities.EnumClassifierTypes;

namespace MyCommerce.Presentation.Stores.Presenters
{
    public class ProductFillPresenter
    {
        private IProductFill pViewToPresent;

        public ProductFillPresenter(IProductFill vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCancel += new EventHandler(pViewToPresent_OnCancel);
            pViewToPresent.OnLoadView += new GenericEventHandler<Product>(pViewToPresent_OnLoadView);
            pViewToPresent.OnSave += new EventHandler(pViewToPresent_OnSave);
            pViewToPresent.OnChargeCategory +=new GenericEventHandler<long>(pViewToPresent_OnChargeCategory);
        }

        void pViewToPresent_OnChargeCategory(object sender, GenericEventArgs<long> e)
        {
            CommonClassifierManager vCommonClassifierManager = new CommonClassifierManager();
            pViewToPresent.ColProductType = vCommonClassifierManager.GetProductTypeByCategory(e.EventData);
        }
        
        private void pViewToPresent_OnSave(object sender, EventArgs e)
        {
            try
            {
                ProductManager vProductManager = new ProductManager();
                Product objProductToSave = pViewToPresent.Get();
                if (pViewToPresent.OptionViewSelect == OptionSelect.Create)
                {
                    vProductManager.SaveProduct(objProductToSave);
                }
                else
                {
                    objProductToSave.IdProduct = pViewToPresent.IdItemEdit;
                    vProductManager.UpdateProduct(objProductToSave);
                }
                pViewToPresent.CloseAndSetResult(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void pViewToPresent_OnLoadView(object sender, GenericEventArgs<Product> e)
        {
            CommonClassifierManager vCommonClassifierManager = new CommonClassifierManager();
            pViewToPresent.ColProductType = vCommonClassifierManager.GetClassifierByIDType((long)CommonClassifierTypes.ProductType);
            pViewToPresent.ColMark = vCommonClassifierManager.GetClassifierByIDType((long)CommonClassifierTypes.ProductMark);
            pViewToPresent.ColCategory = vCommonClassifierManager.GetClassifierByIDType((long)CommonClassifierTypes.ProductCategory);

            if (e.EventData != null)
                pViewToPresent.Set(e.EventData);
        }

        private void pViewToPresent_OnCancel(object sender, EventArgs e)
        {
            pViewToPresent.CloseAndSetResult(false);
        }
    }
}
